﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace Task.SMP
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly string NameService = "AASC";
        public MainWindow()
        {
            InitializeComponent();
            this.label_Message.Content = ServiceStart(NameService);
        }

        private void bt_Install_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsServiceIsExisted(NameService))
                {
                    string CurrentDirectory = System.Environment.CurrentDirectory;
                    System.Environment.CurrentDirectory = CurrentDirectory;
                    Process process = new Process();
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.FileName = "Install.bat";
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();

                    Thread.Sleep(1000);
                    this.label_Message.Content = "服务安装成功！";

                    bt_Start_Click(null, null);
                }
                else
                {
                    this.label_Message.Content = "服务已经安装！";
                }
            }
            catch (Exception ex)
            {
                this.label_Message.Content = ex.Message;
            }
        }

        private void bt_unInstall_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsServiceIsExisted(NameService))
                {
                    string CurrentDirectory = System.Environment.CurrentDirectory;
                    System.Environment.CurrentDirectory = CurrentDirectory;
                    Process process = new Process();
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.FileName = "Uninstall.bat";
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();

                    Thread.Sleep(5000);
                    this.label_Message.Content = "服务卸载成功！";
                }
                else
                {
                    this.label_Message.Content = "服务不存在！";
                }
            }
            catch (Exception ex)
            {
                this.label_Message.Content = ex.Message;
            }
        }

        private void bt_Start_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsServiceIsExisted(NameService))
                {
                    this.label_Message.Content = "服务没有安装！";
                    return;
                }
                if (IsServiceStart(NameService))
                {
                    this.label_Message.Content = "服务已经运行！";
                    return;
                }
                ServiceController serviceController = new ServiceController(NameService);

                serviceController.Start();

                Thread.Sleep(1000);
                this.label_Message.Content = "服务启动成功！";
            }
            catch (Exception ex)
            {
                this.label_Message.Content = ex.Message;
            }
        }

        private void bt_Stop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsServiceIsExisted(NameService))
                {
                    this.label_Message.Content = "服务没有安装！";
                    return;
                }
                if (!IsServiceStart(NameService))
                {
                    this.label_Message.Content = "服务已经停止！";
                    return;
                }
                ServiceController serviceController = new ServiceController(NameService);
                if (serviceController.CanStop)
                {
                    serviceController.Stop();
                    this.label_Message.Content = "服务停止成功！";
                }
                Thread.Sleep(1000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                this.label_Message.Content = ex.Message;
            }
        }

        private void bt_parseAndStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsServiceIsExisted(NameService))
                {
                    this.label_Message.Content = "服务没有安装！";
                    return;
                }
                ServiceController serviceController = new ServiceController(NameService);
                if (serviceController.CanPauseAndContinue)
                {
                    if (serviceController.Status == ServiceControllerStatus.Stopped ||
                        serviceController.Status == ServiceControllerStatus.StopPending)
                    {
                        return;
                    }
                    else if (serviceController.Status == ServiceControllerStatus.Running)
                    {
                        serviceController.Pause();
                        this.label_Message.Content = "服务暂停！";
                    }
                    else if (serviceController.Status == ServiceControllerStatus.Paused)
                    {
                        serviceController.Continue();
                        this.label_Message.Content = "服务继续运行！";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
        }

        #region 检查服务存在的存在性
        /// <summary>
        /// 检查服务存在的存在性
        /// </summary>
        /// <param name=" NameService ">服务名</param>
        /// <returns>存在返回 true,否则返回 false;</returns>
        public static bool IsServiceIsExisted(string NameService)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName.ToLower() == NameService.ToLower())
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 判断window服务是否启动
        /// <summary>
        /// 判断某个Windows服务是否启动
        /// </summary>
        /// <returns></returns>
        public static bool IsServiceStart(string serviceName)
        {
            ServiceController psc = new ServiceController(serviceName);
            bool bStartStatus = false;
            try
            {
                if (!psc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    bStartStatus = true;
                }

                return bStartStatus;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 判断window服务状态
        /// <summary>
        /// 判断某个Windows服务是否启动
        /// </summary>
        /// <returns></returns>
        public static string ServiceStart(string serviceName)
        {
            if (!IsServiceIsExisted(serviceName))
            {
                return "服务没有安装！";
            }
            ServiceController psc = new ServiceController(serviceName);
            try
            {
                if (psc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    return "服务停止！";
                }
                if (psc.Status.Equals(ServiceControllerStatus.Running))
                {
                    return "服务正在运行！";
                }
                return "服务暂停！";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //JobScheduler job = new JobScheduler();
            //job.Start();
        }
    }
}
